package demo3
{
	import copyengine.utils.KeyCode;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;

	import nape.geom.GeomPoly;
	import nape.geom.GeomPolyList;
	import nape.geom.Ray;
	import nape.geom.RayResult;
	import nape.geom.Vec2;
	import nape.phys.Body;
	import nape.phys.Material;
	import nape.shape.Polygon;
	import nape.space.Space;
	import nape.util.ShapeDebug;

	public final class DemoScene_3 extends Sprite
	{
		private var space:Space;
		private var mouseCutManger:MouseCutManger;
		private var physicsSceneLayer:MovieClip;

		private var allBitmapDataBoxVector:Vector.<BitmapDataBox>;

		private var debug:ShapeDebug;

		private var currentPushKeycode:uint;

		private var geomPoly:GeomPoly;

		public function DemoScene_3()
		{
			super();
			this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
		}

		protected function onAddToStage(event:Event):void
		{
			var gravity:Vec2=new Vec2(0, 600);
			space=new Space(gravity);

			allBitmapDataBoxVector=new Vector.<BitmapDataBox>();

			initializePyhsicsScene();

			mouseCutManger=new MouseCutManger();
			mouseCutManger.initialize(this, onReceiveCutMangerCall);
		}

		private function initializePyhsicsScene():void
		{
			physicsSceneLayer=ResUtils.getMC("DemoScene3") as MovieClip;
			this.addChild(physicsSceneLayer);

			//设置底部刚体
			NapeUtils.createStaticBoxBodyByMc(physicsSceneLayer["floorMc"], space);

			//3.创建模拟视图
			debug=new ShapeDebug(this.stage.width, this.stage.height);
			this.addChild(debug.display);

			this.addEventListener(Event.ENTER_FRAME, loop);
			this.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
			this.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
		}

		protected function onKeyboardUp(event:KeyboardEvent):void
		{
			currentPushKeycode=0;
		}

		protected function onKeyboardDown(event:KeyboardEvent):void
		{
			currentPushKeycode=event.keyCode
		}

		protected function onMouseDown(event:MouseEvent):void
		{
			if (currentPushKeycode == KeyCode.CONTROL)
			{
				var box:BitmapDataBox=new BitmapDataBox();
				box.initialize(physicsSceneLayer, event.stageX, event.stageY, space);
				allBitmapDataBoxVector.push(box);
			}
		}

		protected function onReceiveCutMangerCall(_startPoint:Vec2, _endPoint:Vec2):void
		{
			var ray:Ray=Ray.fromSegment(_startPoint, _endPoint);
			var rayResult:RayResult=space.rayCast(ray);
			var parentBody:Body;
			if (rayResult != null)
			{
				parentBody=rayResult.shape.body;
				geomPoly=new GeomPoly((rayResult.shape as Polygon).worldVerts);
				var geomPolyList:GeomPolyList=geomPoly.cut(_startPoint, _endPoint, true, true);

				geomPolyList.foreach(function(s:*):void
				{
					var body:Body=new Body();
					var graphic:Sprite;

					body.shapes.push(new Polygon(s, Material.ice()));
					body.space=space;
				});
			}
		}

		private function loop(e:Event):void
		{
			//Nape空间模拟
			space.step(1 / 60, 30);

			//清除视图
			debug.clear();
			//优化显示图像
			debug.flush();
			//绘制空间
			debug.draw(space);

			for (var i:int=0; i < space.liveBodies.length; i++)
			{
				var imageBox:BitmapDataBox=space.liveBodies.at(i).userData["target"];
				imageBox && imageBox.adjustDisplay();
			}
		}

	}
}
